# coding: utf-8
from PySide6.QtCore import Qt, QUrl, QStandardPaths
from PySide6.QtGui import QDesktopServices, QFont
from PySide6.QtWidgets import QWidget, QFileDialog, QVBoxLayout
from qfluentwidgets import (SwitchSettingCard, PushSettingCard, FluentIcon as FIF,
                            HyperlinkCard, PrimaryPushSettingCard, ScrollArea, InfoBar, TitleLabel, SettingCard,
                            ComboBoxSettingCard, ExpandLayout, setTheme, setThemeColor, setFont, CustomColorSettingCard,
                            SettingCardGroup, OptionsSettingCard)

from common import cfg, isWin11, HELP_URL, FEEDBACK_URL, AUTHOR, VERSION, YEAR, signalBus, SingleIcon
from components import BodyLabel, CustomScrollArea


class SettingInterface(CustomScrollArea):
    """ Setting interface """

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.scrollWidget = QWidget()
        self.verticalLayout = QVBoxLayout(self.scrollWidget)
        self.expandLayout = ExpandLayout()

        # setting label
        self.titleLabel = BodyLabel(self.tr("设置"), self)

        # personalization
        self.personalGroup = SettingCardGroup(self.tr('个性化'), self.scrollWidget)
        self.themeCard = OptionsSettingCard(
            cfg.themeMode,
            FIF.BRUSH,
            self.tr('应用程序主题'),
            '',
            texts=[
                self.tr('明亮'), self.tr('黑暗'),
                self.tr('跟随系统')
            ],
            parent=self.personalGroup
        )
        self.themeColorCard = CustomColorSettingCard(
            cfg.themeColor,
            FIF.PALETTE,
            self.tr('主题色'),
            '',
            parent=self.personalGroup
        )
        self.zoomCard = OptionsSettingCard(
            cfg.dpiScale,
            FIF.ZOOM,
            self.tr("界面缩放"),
            '',
            texts=[
                "100%", "125%", "150%", "175%", "200%",
                self.tr("使用系统设置")
            ],
            parent=self.personalGroup
        )

        # download
        self.downloadGroup = SettingCardGroup(self.tr("下载"), self.scrollWidget)
        self.downloadCard = PushSettingCard(
            self.tr("选择"),
            FIF.FOLDER,
            self.tr("保存文件夹"),
            cfg.get(cfg.downloadFolder),
            self.downloadGroup
        )

        # application
        self.aboutGroup = SettingCardGroup(self.tr('关于'), self.scrollWidget)
        self.helpCard = HyperlinkCard(
            HELP_URL,
            self.tr('打开帮助页面'),
            FIF.HELP,
            self.tr('帮助'),
            self.tr(
                '发现新功能并了解有关 Banana Movie 的有用提示'),
            self.aboutGroup
        )
        self.feedbackCard = PrimaryPushSettingCard(
            self.tr('提供反馈'),
            FIF.FEEDBACK,
            self.tr('提供反馈'),
            self.tr('通过提供反馈帮助我们改进香蕉视频客户端'),
            self.aboutGroup
        )
        self.aboutCard = PrimaryPushSettingCard(
            self.tr('检查更新'),
            SingleIcon.LOGO_PNG,
            self.tr('关于'),
            '© ' + self.tr('版权') + f" {YEAR}, {AUTHOR}. " +
            self.tr('版本') + " v" + VERSION,
            self.aboutGroup
        )

        self.__initWidget()

    def __initWidget(self):
        self.setWidget(self.scrollWidget)
        self.setObjectName('settingInterface')

        setFont(self.titleLabel, 33)
        self.titleLabel.setFixedHeight(100)

        # initialize layout
        self.__initLayout()
        self._connectSignalToSlot()

    def __initLayout(self):
        self.personalGroup.addSettingCard(self.themeCard)
        self.personalGroup.addSettingCard(self.zoomCard)
        self.personalGroup.addSettingCard(self.themeColorCard)

        self.downloadGroup.addSettingCard(self.downloadCard)

        self.aboutGroup.addSettingCard(self.helpCard)
        self.aboutGroup.addSettingCard(self.feedbackCard)
        self.aboutGroup.addSettingCard(self.aboutCard)

        # add setting card group to layout
        self.verticalLayout.setContentsMargins(36, 10, 36, 10)
        self.expandLayout.setSpacing(28)

        self.verticalLayout.addWidget(self.titleLabel)
        self.verticalLayout.addLayout(self.expandLayout)
        self.expandLayout.addWidget(self.personalGroup)
        self.expandLayout.addWidget(self.downloadGroup)
        self.expandLayout.addWidget(self.aboutGroup)

        # adjust icon size
        for card in self.findChildren(SettingCard):
            card.setIconSize(18, 18)

    def _showRestartTooltip(self):
        """ show restart tooltip """
        InfoBar.success(
            self.tr('更新成功'),
            self.tr('配置在重启后生效'),
            duration=1500,
            parent=self
        )

    def _onDownloadFolderChanged(self):
        folder = QFileDialog.getExistingDirectory(
            self, self.tr("选择文件夹"), QStandardPaths.writableLocation(QStandardPaths.StandardLocation.DownloadLocation))

        if not folder or cfg.get(cfg.saveFolder) == folder:
            return

        cfg.set(cfg.saveFolder, folder)
        self.downloadCard.setContent(folder)

    def _connectSignalToSlot(self):
        """ connect signal to slot """
        cfg.appRestartSig.connect(self._showRestartTooltip)

        # personalization
        cfg.themeChanged.connect(setTheme)
        self.themeColorCard.colorChanged.connect(lambda c: setThemeColor(c))

        self.downloadCard.clicked.connect(self._onDownloadFolderChanged)

        # about
        self.feedbackCard.clicked.connect(
            lambda: QDesktopServices.openUrl(QUrl(FEEDBACK_URL)))
